// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "demo.h"
#include "common/common.h"
#include "common/actionmanager/actionmanager.h"
#include "common/actionmanager/actioncontainer.h"
#include "services/window/windowservice.h"
#include "services/editor/editorservice.h"
#include "base/abstractwidget.h"
#include "base/abstractaction.h"
#include "base/abstracteditwidget.h"
#include "base/abstractmenu.h"
#include "widgets/dashboard.h"
#include "widgets/progressbarwidget.h"

#include <QAction>
#include <QIcon>
#include <QToolButton>
#include <QPushButton>
#include <QProgressBar>
#include "widgets/tomatowidget.h"
#include <QSettings>
#include <QDate>
#include "widgets/chartwidget.h"
using namespace std;
using namespace dpfservice;
const char *D_TSET = "test";

void sendEvent();

void Demo::initialize()
{
}

bool Demo::start()
{
    auto windowService = dpfGetService(dpfservice::WindowService);
    // add menu
    auto mTools = ActionManager::instance()->actionContainer(M_TOOLS);
    auto demoMenu = ActionManager::instance()->createContainer("demoMenu");
    demoMenu->menu()->setTitle("demo menu");
    QAction *demoAction = new QAction("demo", this);
    auto cmd = ActionManager::instance()->registerAction(demoAction, "demo.demo");
    demoMenu->addAction(cmd);
    mTools->addMenu(demoMenu);

    if (windowService)
    {
        // 工具栏窗口初始化
        auto floatButton = new TomatoClockButton;
        floatButton->setLogo(QIcon::fromTheme("tomato").pixmap(QSize(20, 20)));
        auto floatButtonImpl = new AbstractWidget(floatButton);
        connect(&dpf::Listener::instance(), &dpf::Listener::pluginsStarted, [=]
                {
                    windowService->addWidgetToTopTool(floatButtonImpl, false, false, Priority::highest + 1);
                    // 初始化配置文件
                    QSettings *m_iniFile = new QSettings("src/plugins/myClock/data.ini", QSettings::IniFormat);
                    QString today = QDate::currentDate().toString("yyyyMMdd");
                    QString today_date = m_iniFile->value("user_base/today_date").toString();
                    QStringList usedDays = m_iniFile->value("user_base/dev_date").toStringList();
                    QStringList usedTime = m_iniFile->value("user_base/dev_time").toStringList();
                    QString dev_day_str = m_iniFile->value("user_base/dev_day").toString();
                    int dev_day_int = dev_day_str.toInt();

                    if (!usedDays.contains(today))
                    {
                        usedDays.append(today);
                        usedTime.append(0);
                        m_iniFile->setValue("user_base/dev_date", usedDays);
                        m_iniFile->setValue("user_base/dev_day", dev_day_int + 1);
                        m_iniFile->setValue("user_base/dev_time", usedTime);
                    }
                    if (today != today_date)
                    {
                        m_iniFile->setValue("user_base/today_date", today);
                        m_iniFile->setValue("user_base/stick_second", 0);
                    } });

        // 面板初始化
        auto dashBoard = new DashBoard(floatButton);
        auto dashboardImpl = new AbstractWidget(dashBoard);
        windowService->registerWidget("dashboard", dashboardImpl);
        windowService->deleteDockHeader("dashboard");
        QToolButton *button = new QToolButton(dashBoard);
        button->setToolTip("dashboard");
        button->setCheckable(true);
        windowService->addStatusBarItem(button);
        auto dashAction = new QAction("Open DashBoard", dashBoard);
        auto dashCmd = ActionManager::instance()->registerAction(dashAction, "DashBoard.Open");
        dashCmd->setDefaultKeySequence(QKeySequence(Qt::ALT + Qt::Key_P));
        connect(dashAction, &QAction::triggered, button, &QToolButton::clicked);
        connect(button, &QToolButton::clicked, this, [=]()
                {
        if (dashBoard->isVisible()) {
            windowService->hideWidget("dashboard");
            button->setChecked(false);
        } else {
            windowService->showWidgetAtPosition("dashboard", dpfservice::Position::Right, false);
            button->setChecked(true);

            } }, Qt::DirectConnection);
        connect(floatButton, &TomatoClockButton::buttonClicked, this, [=]()
                {
                    if (dashBoard->isVisible())
                    {
                        windowService->hideWidget("dashboard");
                        button->setChecked(false);
                    }
                    else
                    {
                        windowService->showWidgetAtPosition("dashboard", dpfservice::Position::Right, false);
                        button->setChecked(true);
                    } 
                    emit updateDashBoard(); }, Qt::DirectConnection);

        QString contextTitle = "DemoContext";
        QString dockTitle = "DemoDock";
        QString mainWindowTitle = "DemoMainWindow1";
        QString displayText = "Hello UnionCode!";

        QAction *action = new QAction(mainWindowTitle, this);
        action->setIcon(QIcon::fromTheme("tomato"));
        windowService->addNavigationItem(new AbstractAction(action), Priority::lowest);

        auto chartWidget = new ChartWidget;
        auto impl = new AbstractWidget(chartWidget);
        windowService->registerWidget(mainWindowTitle, impl);

        // activated when triggered navigation
        connect(action, &QAction::triggered, this, [=]()
                { windowService->showWidgetAtPosition(mainWindowTitle, dpfservice::Position::Central, true); }, Qt::DirectConnection);
    }

    return true;
}

dpf::Plugin::ShutdownFlag Demo::stop()
{
    return Sync;
}

void sendEvent()
{
    dpf::Event event;
    event.setTopic(T_MENU);
    event.setData(D_TSET);
    event.setProperty(P_ACTION_TEXT, "Event has been received successfully!");
    dpf::EventCallProxy::instance().pubEvent(event);
}
